package com.web.report;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

import com.dao.TreatmentTransactionDAO;
import com.util.AuthenticateHttpServlet;

@WebServlet("/Report/TopTreatmentSales")
public class TopTreatmentSalesReport extends AuthenticateHttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		byte[] bytes;
		try {
			String date = request.getParameter("date");
			HashMap map = new HashMap();
			map.put("reportDate", date);
			String path = getServletContext().getRealPath("/WEB-INF/");
//			bytes = JasperRunManager.runReportToPdf(path +"//report//TopSalesTreatmentReport.jasper", map, new EmployeesDAO().getConnection());
			String source = path +"//report//TopSalesTreatmentReport.jrxml";
			JasperReport jasperReport = JasperCompileManager.compileReport(source);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new TreatmentTransactionDAO().getConnection() );
			bytes = JasperExportManager.exportReportToPdf(jasperPrint);
			response.setContentType("application/pdf");
			response.setContentLength(bytes.length);
			ServletOutputStream out = response.getOutputStream();
			out.write(bytes, 0, bytes.length);
			out.flush();
			out.close();
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
